﻿using DAL;
using FastReport;
using FastReport.Preview;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using 纺织贸易管理系统;

namespace Tools
{
   public   class 打印唛头
    { 
        public  string PrinterName { get; set; }
        public  decimal copyies { get; set; }
        public string moban { get; set; }
        public JuanHaoTable juan { get; set; }
        public PrintModel userful { get; set; }
        public void getPrivew(PreviewControl previewControl)
        {
            纺织贸易管理系统.JuanHaoTable juan = new JuanHaoTable();
            var dt = CreatDt();
            dt.Rows.Add();
            赋值(dt, juan);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            FastReport.Report fs = new FastReport.Report();
            fs.RegisterData(ds);
            (new FastReport.EnvironmentSettings()).ReportSettings.ShowProgress = false;
            try
            {
                if (!File.Exists(moban))
                {
                    var arr = moban.Split('\\');
                    ReportService.Save(arr[arr.Length - 1], ReportService.唛头, moban);
                }
                if (File.Exists(moban))
                {
                    fs.Load(moban);
                    fs.Preview = previewControl ;
                    fs.Prepare();
                    fs.ShowPrepared();
                    fs.Preview.ZoomIn();
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowErrorBox ("预览模板的时候发生错误!" + ex.Message);
            }
        }
        public  void 打印( )
        {
            var dt = CreatDt();
            赋值(dt, juan);
            DataSet dataSet = new DataSet();
            dataSet.Tables.Add(dt);
            var fs = new FastReport.Report();
           
            fs.RegisterData(dataSet);     
            try
            {
                if(!File.Exists(moban))
                {
                    var arr = moban.Split('\\');
                    ReportService.Save(arr[arr.Length - 1], ReportService.唛头, moban);
                }
                fs.Load(moban); 
                switch (userful )
                {
                    case PrintModel.Design:
                        fs.Design();
                        var arr = moban .Split('\\');
                        ReportTableService.DeleteReportTable(x => x.reportName == arr[arr.Length - 1] && x.reportStyle == ReportService.唛头);
                        ReportService.LoadReport(moban , new ReportTable { reportStyle = ReportService.唛头, reportName = arr[arr.Length - 1] });
                        break;
                    case PrintModel.Privew:
                        fs.Show();
                        break;
                    case PrintModel.Print:
                        fs.PrintSettings.Printer = PrinterName;
                        fs.PrintSettings.Copies  =(int) copyies;
                        fs.PrintSettings.ShowDialog = false;
                        fs.Print();
                        break;
                }
            }
            catch
            {               
                fs.Design();
            }
        }
        private DataTable CreatDt()
        {
            DataTable dt = new DataTable("唛头信息");
            dt.Columns.Add("编号");
            dt.Columns.Add("品名");
            dt.Columns.Add("规格");
            dt.Columns.Add("克重");
            dt.Columns.Add("门幅");
            dt.Columns.Add("色号");
            dt.Columns.Add("颜色");
            dt.Columns.Add("花号");
            dt.Columns.Add("款号");
            dt.Columns.Add("缸号");
            dt.Columns.Add("卷号");
            dt.Columns.Add("订单号");
            dt.Columns.Add("单位");
            dt.Columns.Add("英文单位", typeof(string));
            dt.Columns.Add("英文名");
            dt.Columns.Add("公斤数");
            dt.Columns.Add("客户名称");
            dt.Columns.Add("工艺名称");
            dt.Columns.Add("客户全称");
            dt.Columns.Add("赠送");
            dt.Columns.Add("等级");
            dt.Columns.Add("长度", typeof(decimal));
            dt.Columns.Add("机台号", typeof(string));
            dt.Columns.Add("检验员", typeof(string));
            dt.Columns.Add("日期", typeof(DateTime));
            dt.Columns.Add("拼接", typeof(string));
            return dt;
        }
        private void 赋值(DataTable dt, JuanHaoTable juan)
        {
            dt.Rows.Add();
            dt.Rows[0]["编号"] = juan.SampleNum;
            dt.Rows[0]["品名"] = juan.SampleName;
            dt.Rows[0]["规格"] = juan.guige;
            dt.Rows[0]["克重"] = juan.Kezhong;
            dt.Rows[0]["门幅"] = juan.Menfu;
            dt.Rows[0]["色号"] = juan.ColorNum;
            dt.Rows[0]["颜色"] = juan.yanse;
            dt.Rows[0]["花号"] = juan.Huahao;
            dt.Rows[0]["款号"] = juan.kuanhao;
            dt.Rows[0]["缸号"] = juan.GangHao;
            dt.Rows[0]["客户全称"] = LXRService.GetOneLXR (x=>x.MC ==(juan.CustomerName??string.Empty ) ).FullName ;
            dt.Rows[0]["客户名称"] = juan.CustomerName ;
            dt.Rows[0]["卷号"] = string.Format("{0:00}", juan.PiHao);
            dt.Rows[0]["工艺名称"] = juan.Houzhengli;
            dt.Rows[0]["等级"] = juan.DengJI;
            //dt.Rows[0]["卷号"] = txtjuanhao.Text;
            dt.Rows[0]["订单号"] = juan.OrderNum;
            dt.Rows[0]["单位"] = juan.Danwei;
            dt.Rows[0]["英文名"] =string.IsNullOrEmpty ( juan.SampleNum)?string.Empty : dbService.GetOnedb(x => x.bh == juan.SampleNum).EnglishName;
            dt.Rows[0]["长度"] = Convert.ToInt32(juan.biaoqianmishu);
            dt.Rows[0]["公斤数"] = Math.Round(juan.Menfu.TryToInt () * juan.Kezhong.TryToInt () * juan.biaoqianmishu / 100000, 2);
            dt.Rows[0]["机台号"] = juan.MachineID;
            dt.Rows[0]["检验员"] = juan.CheckID;
            dt.Rows[0]["日期"] = juan.rq;
            dt.Rows[0]["赠送"] = juan.JiaJian > 0 ? "+" + juan.JiaJian.ToString() : string.Empty;
            dt.Rows[0]["英文单位"] = juan.Danwei =="米"?"M":(string.IsNullOrEmpty (juan.Danwei )?"M":"Y");
            dt.Rows[0]["拼接"] = juan.Splicing ;
        }
    }
}
